/**
 * 查看某个科研题目的详情
 * 
 * /general/topic/detail
 */

 
 
const Topics= require('../../../models/researchTopics')
const User_Info = require('../../../models/usersInfo')
const utils = require('../../../utils/index')
const $ = require('../../../utils/common')

const detail = async function (req, res) {
    try {
        Topics.belongsTo(User_Info,
            {
                foreignKey: 'teacher_user_name',
                targetKey: 'user_name'
            })

        // set a relation with years
        if (!req.body.topic_id) {
            $.sendMessage(res, 1015, '参数topic_id不能缺 少')
            return
        }

        var topic_info = await Topics.findOne({
            where: {
                id: req.body.topic_id
            },
            // attributes: ['id', 'topic_name', 'topic_resource', 'research_direction', 'student_limit',
            // 'topic_status', 'topic_background', 'current_student', 'confirm_num'],
            include: [
                {
                    model: User_Info,
                    attributes: ['office_phone', 'user_phone', 'user_email', 'user_address'],
                }
            ],
            // raw : true 是把对象扁平
            raw:true // 这个属性表示开启原生查询，原生查询支持的功能更多，自定义更强
        })
        

        if (topic_info == null) {
            $.sendMessage(res, 1015, '无效相关数据')
            return
        }

        res.json({
            code: 200,
            message: '获取成功',
            data: topic_info
        })  
        // 日志
        utils.create_a_log(req, '科研题目管理', '浏览课题详情')
        return
    }
    catch(error) {
        res.json({
            message: "抓到异常",
            error: 'error' + error
        })
        utils.create_a_log(req, '科研题目管理', '浏览课题详情', '失败')
        return
    } 
}
    
module.exports = detail